<?php
namespace Home\Controller;
use Think\Controller;
session_start();
//默认主页展示
class IndexController extends Controller {
       public function index(){
       //得到二手商品的数据到关联数组中
        $ershou=D('Ershou');
        $res=$ershou->select();
       //得到求购的数据到关联数组中
        $qiugou=D('Qiugou');
        $qiugou_data=$qiugou->select();
       //得到以物换物的数据到关联数组中
        $exchange=D("Exchange");
        $exchange_data=$exchange->select();

       //分配变量
        $this->assign('data',$res);
        $this->assign('exchange_data',$exchange_data);
        $this->assign('qiugou_data',$qiugou_data);
        //得到登录的用户信息并分配变量
        $user=$_SESSION['username'];
        $this->assign('user',$user);

        //展示主页
        $this->display('index');
       } 

       
       public function logout(){
          session_start();
          session_unset();
         $this->display('logout');
         //注销操作
     }
         //二手精品每件物品的详情
       public function detail(){
         $ershou=D('Ershou');
        //根据主页获取的产品id查询出对应id的每一条数据，得到一个关联数组
         $res=$ershou->where("id=%d",$_GET['id'])->select();
         $this->assign('data',$res);
           
        $user=$_SESSION['username'];
        $this->assign('user',$user);

       	$this->display('detail');
       }

       //求购商品每条信息详情数据获取
       public function qiugou(){
        $qiugou=D('Qiugou');
        $res=$qiugou->where("id=%d",$_GET['id'])->select();
         $this->assign('data',$res);
        $user=$_SESSION['username'];
        $this->assign('user',$user);
        $this->display('qiugou_detail');
       }

       //以物换物商品每条信息详情数据获取
    public function jiaohuan(){
        $jiaohuan=D('Exchange');
        $res=$jiaohuan->where('id=%d',$_GET['id'])->select();
        $this->assign('data',$res);
        $user=$_SESSION['username'];
        $this->assign('user',$user);
        $this->display('jiaohuan_detail');
    }

    //按产品类别搜索
     public function digital(){
       $kind=I("get.kind")?I("get.kind"):"";
       //根据前台传来的id判断其类别
       $data['kind']=$kind;
            switch ($kind) {
              case 0:
                $kind_all="1=1";
                break;
              case 1:
                $kind_item="数码产品";
                break;
                case 2:
                $kind_item="家用电器";
                break;
              case 3:
                $kind_item="二手车";
                break;
              case 4:
                $kind_item="图书音像";
                break;  
                case 5:
                $kind_item="工艺收藏";
                break;  
                 case 6:
                $kind_item="乐器运动";
                break;  
              default:
                $kind_item="";
                break;
            }
          //得到类别后按着二手商品，以物换物，求购信息查询
      $ershou=D('Ershou');
      $res=$kind_all?$ershou->where($kind_all)->select():$ershou->where("kind='%s'",$kind_item)->select();
          $this->assign('ershou_data',$res);

       $exchange=D("Exchange");
       $ex_res=$kind_all?$exchange->where($kind_all)->select():$exchange->where("kind='%s'",$kind_item)->select();
       $this->assign('exchange_data',$ex_res);

       $qiugou=D("Qiugou");
       $qiu_res=$kind_all?$qiugou->where($kind_all)->select():$qiugou->where("kind='%s'",$kind_item)->select();
      $this->assign('qiugou_data',$qiu_res);


      $user=$_SESSION['username'];
        $this->assign('user',$user);
        $this->assign('search',$kind_item);

          $this->display('search');  
     } 
     //按区域搜索
     public function area(){
      switch (I('get.local')) {
              case 0:
                $local_all="1=1";
                break;
              case 1:
                $local_item="桥西区";
                break;
              case 2:
                $local_item="裕华区";
                break;
              case 3:
                $local_item="长安区";
                break;
              case 4:
                $local_item="桥东区";
                break;  
              case 5:
                $local_item="新华区";
                break;  
              case 6:
                $local_item="正定区";
                break;  
              default:
                $local_item="1111";
                break;
            }

      $ershou=D('Ershou');
      $res=$local_all?$ershou->where($local_all)->select():$ershou->where("area='%s'",$local_item)->select();
          $this->assign('ershou_data',$res);

       $exchange=D("Exchange");
       $ex_res=$local_all?$exchange->where($local_all)->select():$exchange->where("area='%s'",$local_item)->select();
       $this->assign('exchange_data',$ex_res);

       $qiugou=D("Qiugou");
       $qiu_res=$local_all?$qiugou->where($local_all)->select():$qiugou->where("area='%s'",$local_item)->select();
      $this->assign('qiugou_data',$qiu_res);

        $user=$_SESSION['username'];
        $this->assign('user',$user);
        $this->assign('search',$local_item);

          $this->display('search');  
     }

//模糊查询按着产品的描述
        public function search(){
          $want=I('post.want');

          $condition['desc']=array('like','%'.$want.'%');

          $ershou=D('Ershou');
          $res=$ershou->where($condition)->select();
          $this->assign('ershou_data',$res);

       $exchange=D("Exchange");
       $ex_res=$exchange->where($condition)->select();
       $this->assign('exchange_data',$ex_res);

       $qiugou=D("Qiugou");
       $qiu_res=$qiugou->where($condition)->select();
      $this->assign('qiugou_data',$qiu_res);

       $user=$_SESSION['username'];
        $this->assign('user',$user);
        $this->assign('search',$want);

          $this->display('search');  

        }
}
